package logic

import (
	"context"
	"fmt"
	"hire/hirerpc/hirerpc"
	"hire/hirerpc/internal/svc"
	"hire/models/cache"
	"hire/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type HireLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHireLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HireLogic {
	return &HireLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HireLogic) Hire(in *hirerpc.HireRequest) (*hirerpc.HireResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SmsCode {
		return nil, fmt.Errorf("短信验证码错误，请重新发送")
	}
	var hire mysql.Hire
	if hire.ID == 0 {
		hire = mysql.Hire{
			Mobile:   in.Mobile,
			Password: in.Password,
		}
		mysql.DB.Debug().Create(&hire)
	} else {
		if hire.Password != in.Password {
			return nil, fmt.Errorf("当前密码错误，请重新输入")
		}
	}

	return &hirerpc.HireResponse{
		HireId: int64(hire.ID),
	}, nil
}
